package com.hdu.atomic;

import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

@Slf4j
@Data
public class AtomicUpdater {


    public static void main(String[] args) {
        final String FIELD_NAME = "name";

        Student student = new Student();


        AtomicReferenceFieldUpdater<Student, String> updater =
                AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, FIELD_NAME);

        boolean flag = updater.compareAndSet(
                student,
                null,
                "张三"
        );
        if (flag) {
            log.info("更新成功");
            System.out.println(student);
        } else {
            log.info("更新失败");
        }
    }

}


@ToString
class Student {
    public volatile String name;
}
